#ifndef __DEBUG_H__
#define __DEBUG_H__

#include "microThread_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DBGBUG_ERROR           0
#define DBGBUG_WARNING         1
#define DBGBUG_INFO            2
#define DBGBUG_LOG             3

#ifndef DBGBUG_LEVEL
#define DBGBUG_LEVEL DBGBUG_WARNING
#endif

#ifndef LOG_RAW
#include <stdio.h>
#define LOG_RAW printf
#endif

#ifndef DBGBUG_TAG
#define DBGBUG_TAG ""
#endif

#define LOG_LINE(lvl, fmt, ...) LOG_RAW("["lvl"/"DBGBUG_TAG"]"fmt, ##__VA_ARGS__)

/* 输出错误 */
#if DBGBUG_LEVEL >= DBGBUG_ERROR
#define LOG_E(fmt, ...) LOG_LINE("E", fmt, ##__VA_ARGS__)
#else
#define LOG_E(fmt, ...) {}
#endif

/* 输出警告 */
#if DBGBUG_LEVEL >= DBGBUG_WARNING
#define LOG_W(fmt, ...) LOG_LINE("W", fmt, ##__VA_ARGS__)
#else
#define LOG_W(fmt, ...) {}
#endif

/* 输出信息 */
#if DBGBUG_LEVEL >= DBGBUG_INFO
#define LOG_I(fmt, ...) LOG_LINE("I", fmt, ##__VA_ARGS__)
#else
#define LOG_I(fmt, ...) {}
#endif

/* 输出调试 */
#if DBGBUG_LEVEL >= DBGBUG_LOG
#define LOG_D(fmt, ...) LOG_LINE("D", fmt, ##__VA_ARGS__)
#else
#define LOG_D(fmt, ...) {}
#endif

/* 断言，条件为假通过 */
#ifdef MT_ENABLE_ASSERT
#include "thread.h"
#define MT_ASSERT(EX)                                     \
    do{                                                   \
        if(EX)                                            \
        {                                                 \
            LOG_RAW(#EX";%s,%d\r\n", __FILE__, __LINE__); \
            mt_interrupt_save();                          \
            while(1);                                     \
        }                                                 \
    }while(0)
#else
#define MT_ASSERT(EX) {}
#endif

/* 线程上下文检测 */
#ifdef MT_ENABLE_CONTEXT_CHECK
#include "thread.h"
extern mt_thread_t *current_thread;
extern mt_int32_t interrupt_nest;
#define MT_CHECK_THREAD_CONTEXT()                  \
    do{                                            \
        MT_ASSERT(interrupt_nest != 0);       \
        MT_ASSERT(current_thread == MT_NULL); \
    }while(0)
#else
#define MT_CHECK_THREAD_CONTEXT() {}
#endif


#ifdef __cplusplus
}
#endif

#endif
